package com.bwie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.bwie.common.GetNum;
import com.bwie.common.constants.LoginConstants;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.*;
import com.bwie.common.domain.request.GroupRequest;
import com.bwie.common.domain.request.GroupUserRequest;
import com.bwie.common.domain.request.MerchantRequest;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import com.bwie.exception.GroupException;
import com.bwie.mapper.GroupMapper;
import com.bwie.service.GroupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: GroupServiceImp
 * @Description: TODO
 * @version: v1.８.0
 * @author: 张雨乐
 */
@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    HttpServletRequest request;

    @Override
    public Result merchantList(MerchantRequest merchantRequest) {
        PageHelper.startPage(merchantRequest.getPageNum(), merchantRequest.getPageSize());
        List<Merchant>list=groupMapper.merchantList(merchantRequest);
        PageInfo<Merchant> info = new PageInfo<>(list);
        return Result.success(info);
    }


    @Override
    public Result dishList(String groupId) {
        List<Dish>list=groupMapper.dishList(groupId);
        return Result.success(list);
    }


    /**
     * 查看用户团购记录
     * @param groupUserRequest
     * @return
     */
    @Override
    public Result groupUser(GroupUserRequest groupUserRequest) {
        PageHelper.startPage(groupUserRequest.getPageNum(), groupUserRequest.getPageSize());
        List<GroupUser>list=groupMapper.groupUser(groupUserRequest);
        PageInfo<GroupUser> info = new PageInfo<>(list);
        return Result.success(info);
    }

    /**
     * 添加团购记录
     * @param groupUserRequest
     * @return
     */
    @Override
    public Result groupUserAdd(GroupUserRequest groupUserRequest) {
        String phone = groupUserRequest.getPhone();
        Integer groupId = groupUserRequest.getGroupId();
        Integer num = groupUserRequest.getNum();
        if (StrUtil.isBlank(phone)){
            throw new GroupException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        if (groupId==null){
            throw new GroupException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        if (num==null){
            throw new GroupException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        User user = (User) redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        if (user==null){
            throw new GroupException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        groupUserRequest.setUserId(user.getId());
        groupMapper.groupUserAdd(groupUserRequest);
        return Result.success("添加成功");
    }

    @Override
    public Result getNum(String id) {
        List<GetNum>list=groupMapper.getNum(id);
//        Num num = new Num();
//        ArrayList<Integer> list1 = new ArrayList<>();
//        ArrayList<String> list2 = new ArrayList<>();
//        for (GetNum getNum : list) {
//            list1.add(getNum.getValue());
//            list2.add(getNum.getName());
//        }
//        num.setName(list2);
//        num.setValue(list1);
        return Result.success(list);
    }


    @Override
    public Result groupList(GroupRequest groupRequest) {
        PageHelper.startPage(groupRequest.getPageNum(), groupRequest.getPageSize());
        List<Group>list=groupMapper.groupList(groupRequest);
        PageInfo<Group> info = new PageInfo<>(list);
        return Result.success(info);
    }



}
